package babo.cd.physicalexperiment.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import babo.cd.physicalexperiment.BaseActivity;
import babo.cd.physicalexperiment.MainActivity;
import babo.cd.physicalexperiment.app.MyApplication;
import babo.cd.physicalexperiment.bean.UserInfoBean;
import babo.cd.physicalexperiment.data.UserInfo;
import babo.cd.physicalexperiment.databinding.ActivityLoadingBinding;
import babo.cd.physicalexperiment.utils.OKCallback;
import babo.cd.physicalexperiment.utils.OkUtils;
import babo.cd.physicalexperiment.utils.Out;
import babo.cd.physicalexperiment.utils.URLS;

public class LoadingActvity extends BaseActivity {

    ActivityLoadingBinding binding;


    @Override
    protected View bindView() {
        binding=ActivityLoadingBinding.inflate(getLayoutInflater());
        return binding.getRoot();

    }

    @Override
    protected void initView() {

    }

    @Override
    protected void loadData() {

        String uid=sharedPreferences.getString("uid","");
        String token=sharedPreferences.getString("token","");
        if (TextUtils.isEmpty(uid)||TextUtils.isEmpty(token)){
            hd.postDelayed(new Runnable() {
                @Override
                public void run() {
                    Intent intent=new Intent(context,LoginActivity.class);
                    startActivity(intent);
                    finish();
                }
            },1500);
        }else{
            UserInfo.userId=uid;
            UserInfo.JsessionId=token;
            getUserInfo();
        }



    }


    private void getUserInfo() {
        Out.out("获取用户信息啦");
        OkUtils.GetOk(URLS.getUserInfo,new OKCallback(context){
            @Override
            public void Eorror(int code) {
                super.Eorror(code);
                Out.out("获取用户报错信息啦");
                if (code==401){
                    UserInfo.userId="";
                    UserInfo.JsessionId="";
                    Intent intent=new Intent(context,LoginActivity.class);
                    startActivity(intent);
                    finish();
                }
            }

            @Override
            public void Sucess(String str) {
                super.Sucess(str);
                Out.out("用户信息："+str);
                UserInfoBean userInfoBean=gson.fromJson(str,UserInfoBean.class);
                MyApplication.getInstance().setUserBaseInfo(userInfoBean.getData());
                if (TextUtils.isEmpty(userInfoBean.getData().getName())){
                    hd.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            goToNextActivity(LoginActivity.class);
                            goToNextActivity(UploadBaseInfoActivity.class);
                            finish();
                        }
                    },1000);

                }else{
                    hd.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            goToNextActivity(LoginActivity.class);
                            goToNextActivity(MainActivity.class);
                            finish();
                        }
                    },1500);

                }


            }

        });
    }
}
